<?php
namespace Admin\Controller;
class ArticleController extends BaseController {
    public function index(){
    	$a = M('Article');
    	$c = M('Column');
        $list = $a->select();
    	$count = $a->count();
    	foreach ($list as $key => $value) {
    		$data[] = $c->field('name')->where('id = '.$value['cid'])->find();
    	}
    	$list1 = $c->where()->select();
		
    	$this->assign('list',$list);
    	$this->assign('count',$count);
    	$this->assign('data',$data);
        $this->display();
    }

    //添加文章
    public function adddo(){
    	$a = M('Article');
    	$_POST['addtime']=time();
        $_POST['status'] =0;

	    $result = $a->add($_POST);
        if($result){
            $this->success('文章添加成功');
        }else{
            $this->error('文章添加失败');
        }

	}
    	
    //文章列表
    public function add(){
        $data = M('column')->select();
        $this->assign('data',$data);
        $this->display();
    }
    //文章图片
    public function add_photo1(){
        $upload = new \Think\Upload();                                          // 实例化上传类
        $upload->maxSize = 1024*1024*10 ;                                       // 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');                     // 设置附件上传类型
        $upload->rootPath = 'Public/';                                       // 设置附件上传根目录
        $upload->savePath = 'admin/Uploads/images/';
        $info = $upload->uploadOne($_FILES['img1']);
        if($info){
            $res['messsge']=$upload->getError();
            $res['result_code'] = 100;
            $res['file'] = '/public/'.$info['savepath'].$info['savename'];
        }else{
             $res['messsge']=$upload->getError();
            $res['result_code'] = 101;
        }
        $this->ajaxReturn($res);
    }
        //修改文章
    public function edit(){
        if(IS_GET){
        $id=I('id');
        $column  = M('column')->select();
        $data=M('Article')->where('id = '.$id)->find();
        $this->assign('data',$data);
        $this->assign('column',$column);
        $this->display();
     }else{
        $res=M('article')->save($_POST);
        if($res){
            $this->success('修改成功',U('Article/index'));
        }else{
            $this->error('修改失败');
        }
     }
       
    }

    //删除
    public function del(){
       $res=M('Article')->where($_POST)->delete();
       if($res){
        $this->ajaxReturn(1);
       }else{
        $this->ajaxReturn(0);
       }
    }
    //修改状态
    public function status(){
        $result=M('Article')->save($_POST);
        if($result){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(0);
        }
    }
}